const { axios } = require('../../utils/http')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify.js'

Page({
  data: {
    comment: 'comment',
    commentList: [],
    active: 1,
    sms: ''
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 1
      })
    }
    this.getCommentList()
  },
  getCommentList() {
    axios.get('/advice/search').then(res => {
      console.log(res.data.data)
      this.setData({
        commentList: res.data.data
      })
    })
  },
  sendMessage() {
    if (!this.data.sms.trim()) {
      Notify({
        message: '请输入留言',
        selector: '#van-notify'
      })
      return
    }
    const { username } = wx.getStorageSync('userInfo')
    axios
      .post('/advice/add', {
        content: this.data.sms,
        username
      })
      .then(res => {
        console.log(res)
        if (res.data.error === -1) {
          Notify({
            type: 'primary',
            message: '添加成功',
            selector: '#van-notify'
          })
          this.getCommentList()
          this.setData({
            sms: ''
          })
        } else {
          Notify('添加失败')
        }
      })
  }
})
